package com.example.movie.data.movie.model

import android.util.Log
import com.example.movie.common.exception.RemoteServerException
import com.example.movie.common.model.BaseResponse
import com.example.movie.common.model.Result
import com.example.movie.common.model.ResultCode
import com.example.movie.common.request.MovieService
import com.example.movie.data.movie.model.bean.ApproveBean
import com.example.movie.data.movie.model.vo.*
import com.example.movie.utils.RequestUtil
import java.io.IOException
import java.net.UnknownServiceException


/**
 * Class that handles authentication w/ login credentials and retrieves user information.
 */
class MovieDataSource {

    private val movieService: MovieService = RequestUtil.getService(MovieService::class.java)

    fun loadIndexData(): Result<MovieListVo> {
        return try {
            val res = movieService.index().execute().body()!!
            if (res.success) {
                Result.Success(res.data)
            } else {
                when (res.code) {
                    ResultCode.REMOTE_SERVER_ERROR.value -> throw RemoteServerException(res.message)
                    else -> throw UnknownServiceException("未知异常!")
                }
            }
        } catch (e: Throwable) {
            Log.e("request error", e.toString())
            Result.Error(IOException("Error request", e))
        }
    }


    fun getMovieDetailData(movieId: Int): Result<MovieDetailVo>? {
        return try {
            val res = movieService.movieDetail(movieId).execute().body()!!
            if (res.success) {
                Result.Success(res.data)
            } else {
                when (res.code) {
                    ResultCode.REMOTE_SERVER_ERROR.value -> throw RemoteServerException(res.message)
                    else -> throw UnknownServiceException("未知异常!")
                }
            }
        } catch (e: Throwable) {
            Log.e("request error", e.toString())
            Result.Error(IOException("Error request", e))
        }
    }

    fun search(key: String): Result<List<SearchVo>>? {
        return try {
            val res = movieService.search(key).execute().body()!!
            if (res.success) {
                Result.Success(res.data)
            } else {
                when (res.code) {
                    ResultCode.REMOTE_SERVER_ERROR.value -> throw RemoteServerException(res.message)
                    else -> throw UnknownServiceException("未知异常!")
                }
            }
        } catch (e: Throwable) {
            Log.e("request error", e.toString())
            Result.Error(IOException("Error request", e))
        }

    }

    fun changeUserAndMovieState(movieId: Int, type: Int): Result<Boolean>? {
        return try {
            val res = movieService.changeUserAndMovieState(movieId,type).execute().body()!!
            if (res.success) {
                Result.Success(true)
            } else {
                when (res.code) {
                    ResultCode.REMOTE_SERVER_ERROR.value -> throw RemoteServerException(res.message)
                    else -> throw UnknownServiceException("未知异常!")
                }
            }
        } catch (e: Throwable) {
            Log.e("request error", e.toString())
            Result.Error(IOException("Error request", e))
        }
    }

    fun addForum(movieId: Int, forum: String, pid: Int?, toId: String?): Result<Int>? {
        return try {
            val res = movieService.addForum(movieId, forum, pid, toId).execute().body()!!
            if (res.success) {
                Result.Success(res.data)
            } else {
                when (res.code) {
                    ResultCode.REMOTE_SERVER_ERROR.value -> throw RemoteServerException(res.message)
                    else -> throw UnknownServiceException("未知异常!")
                }
            }
        } catch (e: Throwable) {
            Log.e("request error", e.toString())
            Result.Error(IOException("Error request", e))
        }
    }

    fun approve(forumId: Int): Result<ApproveBean>? {
        return try {
            val res = movieService.approve(forumId).execute().body()!!
            if (res.success) {
                Result.Success(res.data)
            } else {
                when (res.code) {
                    ResultCode.REMOTE_SERVER_ERROR.value -> throw RemoteServerException(res.message)
                    else -> throw UnknownServiceException("未知异常!")
                }
            }
        } catch (e: Throwable) {
            Log.e("request error", e.toString())
            Result.Error(IOException("Error request", e))
        }
    }

    fun loadForumPage(movieId: Int, page: Int?, size: Int?): Result<BaseResponse<List<ForumVo>>>? {
        return try {
            val res = movieService.loadForumPage(movieId, page, size).execute().body()!!
            if (res.success) {
                Result.Success(res)
            } else {
                when (res.code) {
                    ResultCode.REMOTE_SERVER_ERROR.value -> throw RemoteServerException(res.message)
                    else -> throw UnknownServiceException("未知异常!")
                }
            }
        } catch (e: Throwable) {
            Log.e("request error", e.toString())
            Result.Error(IOException("Error request", e))
        }
    }

    fun loadReplies(pid: Long, page: Int, size: Int): Result<BaseResponse<List<ForumVo>>>? {
        return try {
            val res = movieService.loadReplies(pid, page, size).execute().body()!!
            if (res.success) {
                Result.Success(res)
            } else {
                when (res.code) {
                    ResultCode.REMOTE_SERVER_ERROR.value -> throw RemoteServerException(res.message)
                    else -> throw UnknownServiceException("未知异常!")
                }
            }
        } catch (e: Throwable) {
            Log.e("request error", e.toString())
            Result.Error(IOException("Error request", e))
        }
    }

    fun loadMovieTopList(): Result<List<MovieVo>>? {
        return try {
            val res = movieService.loadMovieTopList().execute().body()!!
            if (res.success) {
                Result.Success(res.data)
            } else {
                when (res.code) {
                    ResultCode.REMOTE_SERVER_ERROR.value -> throw RemoteServerException(res.message)
                    else -> throw UnknownServiceException("未知异常!")
                }
            }
        } catch (e: Throwable) {
            Log.e("request error", e.toString())
            Result.Error(IOException("Error request", e))
        }
    }

}